using System;
using System.Text;

namespace NhibernateDemoCore
{
    public class Address
    {
        public virtual string Line1 { get; set; }
        public virtual string Line2 { get; set; }
        public virtual string City { get; set; }
        public virtual string State { get; set; }
        public virtual string Zip { get; set; }

        public override string ToString()
        {
            var builder = new StringBuilder();
            
            builder.AppendLine(Line1);
            if(string.IsNullOrEmpty(Line2) == false)
            {
                builder.AppendLine(Line2);
            }
            builder.AppendFormat("{0} {1} {2}", City, State, Zip);

            return builder.ToString();
        }

        public bool Equals(Address other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.Line1, Line1) 
                && Equals(other.Line2, Line2) 
                && Equals(other.City, City) 
                && Equals(other.State, State) 
                && Equals(other.Zip, Zip);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return obj.GetType() == typeof (Address) && Equals((Address) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                var result = (Line1 != null ? Line1.GetHashCode() : 0);
                result = (result*397) ^ (Line2 != null ? Line2.GetHashCode() : 0);
                result = (result*397) ^ (City != null ? City.GetHashCode() : 0);
                result = (result*397) ^ (State != null ? State.GetHashCode() : 0);
                result = (result*397) ^ (Zip != null ? Zip.GetHashCode() : 0);
                return result;
            }
        }

        public static bool operator ==(Address left, Address right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(Address left, Address right)
        {
            return !Equals(left, right);
        }
    }
}